#version 460
#extension GL_GOOGLE_include_directive : enable
#extension GL_EXT_nonuniform_qualifier : enable
#extension GL_EXT_shader_explicit_arithmetic_types_int64 : require

#include "../../../../core/shader_std.h"


layout(location = 0) in vec4 vColor;
layout(location = 1) in vec2 vUV;
layout(location = 2) in flat float gap;
layout(location = 3) in flat float offset;
layout(location = 4) in flat uint shaderType;
layout(location = 5) in flat uint offset_texID;
layout(location = 6) in flat uint texNum;

layout(location=0) out vec4 outColor;
layout(location=1) out vec4 outColorB;
layout(depth_greater) out float gl_FragDepth;

layout(set = 4, binding = DEF_binding_L4_TexArray) uniform sampler2D texArray[];

void main(void) {
	
	switch(shaderType){
		case DEF_2DShaderType_AlphaStroke : {
			vec2 texStp = 1.0 / vec2(textureSize(texArray[nonuniformEXT(offset_texID)], 0));
			
			float h[9];
			uint index = 0;
			for (int y = -1; y<=1; ++y) {
				for (int x = -1; x<=1; ++x) {
					float s = texture(texArray[nonuniformEXT(offset_texID)], vUV + texStp * vec2(x, y)).a;
					h[index] = s;
					++index;
				}
			}
			// 计算最大Alpha差异（捕捉边缘）
			float maxDiff = 0.0;
			for (int i = 0; i < 9; i++) {
				maxDiff = max(maxDiff, abs(h[4] - h[i]));
			}

			float edgeIntensity = smoothstep(0.1, 0.1 + 0.9, maxDiff);
    		edgeIntensity *= smoothstep(0.2, 1.0, h[4]);
			
			//float mag = sqrt(pow(hv.x, 2) + pow(hv.y, 2));
			outColor = vec4(vec3(vColor), edgeIntensity);
			break;
		}
		default : {
			outColor = abs(texture(texArray[nonuniformEXT(offset_texID)], vUV)); 
		}
	}
	//outColor = vec4(1);
	//outColor.rg = vUV;
	//outColor.b = offset_texID;
	//outColor.a = 1;
	//outColor = vec4(vec3(outColor.a), outColor.a);
	//outColor.a = 0.5;

	outColorB = vec4(0);
}


 